package cn.iocoder.yudao.module.custom.service.merchantkey;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;

import cn.iocoder.yudao.module.custom.controller.admin.merchantkey.vo.*;
import cn.iocoder.yudao.module.custom.dal.dataobject.merchantkey.MerchantKeyDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.custom.convert.merchantkey.MerchantKeyConvert;
import cn.iocoder.yudao.module.custom.dal.mysql.merchantkey.MerchantKeyMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.custom.enums.ErrorCodeConstants.*;

/**
 * 商户私钥 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class MerchantKeyServiceImpl implements MerchantKeyService {

    @Resource
    private MerchantKeyMapper merchantKeyMapper;

    @Override
    public Integer createMerchantKey(MerchantKeyCreateReqVO createReqVO) {
        // 插入
        MerchantKeyDO merchantKey = MerchantKeyConvert.INSTANCE.convert(createReqVO);
        merchantKeyMapper.insert(merchantKey);
        // 返回
        return merchantKey.getId();
    }

    @Override
    public void updateMerchantKey(MerchantKeyUpdateReqVO updateReqVO) {
        // 校验存在
        validateMerchantKeyExists(updateReqVO.getId());
        // 更新
        MerchantKeyDO updateObj = MerchantKeyConvert.INSTANCE.convert(updateReqVO);
        merchantKeyMapper.updateById(updateObj);
    }

    @Override
    public void deleteMerchantKey(Integer id) {
        // 校验存在
        validateMerchantKeyExists(id);
        // 删除
        merchantKeyMapper.deleteById(id);
    }

    private void validateMerchantKeyExists(Integer id) {
        if (merchantKeyMapper.selectById(id) == null) {
            throw exception(MERCHANT_KEY_NOT_EXISTS);
        }
    }

    @Override
    public MerchantKeyDO getMerchantKey(Integer id) {
        return merchantKeyMapper.selectById(id);
    }

    @Override
    public List<MerchantKeyDO> getMerchantKeyList(Collection<Integer> ids) {
        return merchantKeyMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<MerchantKeyDO> getMerchantKeyPage(MerchantKeyPageReqVO pageReqVO) {
        return merchantKeyMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MerchantKeyDO> getMerchantKeyList(MerchantKeyExportReqVO exportReqVO) {
        return merchantKeyMapper.selectList(exportReqVO);
    }

}
